package com.ddch.udpsocket.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ddch.udpsocket.R;
import com.ddch.udpsocket.socket.udp.UDPSocket;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * created by 韦敏敏
 * on 2019/12/18
 */
public class UdpSocketActivity extends AppCompatActivity {
    private UDPSocket socket;
    private TextView tvShow;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0){
                String strContent = (String) msg.obj;
                tvShow.setText(strContent);
            }

        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_udp_socket_layout);
        socket = new UDPSocket(this);
        socket.startUDPSocket(mHandler);
        tvShow = findViewById(R.id.msgShow);
        final EditText etMessage = findViewById(R.id.et_message);
        findViewById(R.id.bt_send).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                socket.sendMessage(etMessage.getText().toString());
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        socket.stopUDPSocket();
    }
}
